

import java.io.IOException;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import domein.Klant;
import domein.Personeel;

/**
 * Servlet Filter implementation class SecurityFilter
 */
@WebFilter(urlPatterns = { "/medewerker/*" })
public class MedewerkerSecurityFilter implements Filter {

    /**
     * Default constructor. 
     */
    public MedewerkerSecurityFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest r2 = (HttpServletRequest)request;
		Object user = r2.getSession().getAttribute("user");
		if(user == null || !(user instanceof Personeel) || !((Personeel)user).getFunctie().equals("baliemedewerker")){
			request.setAttribute("error","U dient in te loggen als baliemedewerker om deze pagina te kunnen bezoeken");
			r2.getRequestDispatcher("/login.jsp").forward(request, response);
		}
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
